package com.weetop.cfw.base.presenter.imp

import com.weetop.cfw.base.presenter.MessagePresenter
import com.weetop.cfw.base.ui.activity.CommonBaseActivity
import com.weetop.cfw.base.ui.fragment.CommonBaseFragment
import com.weetop.cfw.base.view.BaseView
import com.weetop.cfw.base.view.MessageView
import com.weetop.cfw.bean.MessageListBean
import com.weetop.cfw.callback.RxJavaCallBack
import com.weetop.cfw.message.fragment.MessageFragment
import com.weetop.cfw.utils.RetrofitUtils
import com.weetop.cfw.utils.RxJavaUtils

class MessagePresenterImp(var messageView: MessageView? = null):MessagePresenter {


    /**
     * 获取消息列表
     */
    override fun getMessageList(commonBaseFragment: CommonBaseFragment, messageCate: String, currentPage: Int) {
        if(messageCate == "1"){
            commonBaseFragment.showLoadingDialog(commonBaseFragment.activity as CommonBaseActivity,"正在获取系统消息...")
        }else if(messageCate == "2"){
            commonBaseFragment.showLoadingDialog(commonBaseFragment.activity as CommonBaseActivity,"正在获取消费消息...")
        }
        RxJavaUtils.useInSupportFragmentOnDestoryView(RetrofitUtils.getApi().getMessageList(msgcate = messageCate,curpage = currentPage),commonBaseFragment,object:RxJavaCallBack<MessageListBean>(){

            override fun onSuccess(t: MessageListBean?) {
                commonBaseFragment.dismissLoadingDialog()
                messageView?.messageListGetSuccess(t)
            }

            override fun onRequestError(t: MessageListBean?) {
                super.onRequestError(t)
                commonBaseFragment.dismissLoadingDialog()
            }

            override fun onErrorResponse(throwable: Throwable?) {
                super.onErrorResponse(throwable)
                commonBaseFragment.dismissLoadingDialog()
            }

        })
    }

    /**
     * 绑定
     */
    override fun attachView(baseView: BaseView) {
        messageView = baseView as? MessageView
    }

    /**
     * 解绑
     */
    override fun detachView() {
        messageView = null
    }
}